<!DOCTYPE html>
<html>
<head>
<!-- <meta charset="UTF-8" />
<meta http-equiv="pragma" content="no-cache"></meta>
<meta http-equiv="cache-control" content="no-cache"></meta>
<meta http-equiv="expires" content="0"></meta>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link th:href="${quickJs}+'scripts/layui/css/layui.css'" rel="stylesheet" />
<script th:src="${quickJs}+'scripts/jquery/jquery-1.11.2.min.js'"></script>
<script th:src="${quickJs}+'scripts/layui/layui.js'"></script> -->
<div th:include="base-libs :: base-libs" />
</head>

<title>变更服务包</title>

<style>

.fwbLi{
	display: inline-block;
	border: 1px solid #D4D4D4;
	vertical-align: middle;
	padding-right: 8px;
	margin-right: 10px;
	margin-bottom: 4px;
	margin-top: 4px;
}
.fwbLi label{
	display: inline-block;
	height: 30px;
	line-height:30px;
	padding: 0px 6px;
	margin-right: 4px;
	text-align: center;
	background-color: #f7f9fb;
	border-right: 1px solid #D4D4D4;
}
.fwbLi .fwbsx span{
	display: inline-block;
	height: 100%;
	cursor: pointer;
}

.fwbLi input{
	display: inline-block;
	width: 20px!important;
	height: 20px!important;
	vertical-align: middle;
	margin-bottom: 2px;
}
.rqflUl .jmrqflInput {
	width: 20px!important;
	height: 20px!important;
	position: relative;
	float: left;
	top: 5px;
	left: 15px;
}
tr:hover{
	  background:#fff !important;
}
</style>
<script th:inline="javascript">
	/**读取request中的初始化数据*/
	var result = [[${DATA}]];
</script>

<script type="text/javascript">
	var bgfwbInfo = StoreCache.removeCache("JTQYBGFWBINFO");
	var QYXXIDS = [];
	var FWBIDS = [];
	var FWBSX = [];
    var newFwbid = [];
    var newFwbsx = [];
    var JMXXIDS = [],TDIDS = [],TDMCS = [];
	$(function(){
		initView();
		initData();
	});

	function initData(){
        $("#JLS").html("您已经选择了 " + result.RS + "记录");
        for(var i = 0; i < bgfwbInfo.length ; i ++){
            var row = bgfwbInfo[i];
            QYXXIDS.push(row["QYXXID"]);
            FWBIDS.push(row["FWBID"]);
            FWBSX.push(row["FWBSX"]);
            JMXXIDS.push(row["JMXXID"]);
            TDIDS.push(row["YLTDID"]);
            TDMCS.push(row["TDMC"]);
            var tr = $("<tr>").addClass("item");
            var td1 = $("<td align='center'>").html(row["XM"]);
            var td2 = $("<td>").html(row["FWBMC"]);
            var td3 = $("<td>").css("padding","5px 7px");

            var div = $("<ul>").attr({"class":"fwbUl","quick-config":"validateProxy:'validateFwb'"});
            $.each(result.rows, function(index, item){
                var fwbLi = $($("#fwbLi").html());
                $(fwbLi).find("label").text(item["FWBMC"]);
                $(fwbLi).find(".jmrqflInput").attr("value" , item["FWBID"]).addClass(item["FWBID"])
                    .data({"SFBHYBFWX":item["SFBHYBFWX"], "YBJJZF":item["YBJJZF"]});
                $(fwbLi).find(".fwbsx").hide();
                $(fwbLi).find(".fwbsx span").on("click", function(){
                    $(this).find("input:radio").prop("checked", true);
                });
                $(fwbLi).find("input:radio").attr("name", item["FWBID"]);
                //服务包类型区别
                if(item.FWBLX == '03'){
                    $(div).append(fwbLi);
                }else{
                    $(fwbLi).find(".jmrqflInput").attr("type","radio").addClass("dxFwb");
                    $(div).prepend(fwbLi);
                }
                $(fwbLi).find(".jmrqflInput").click(function(){
					/*单选框服务包处理*/
                    var isRadio = $(this).attr("type");
                    if(isRadio == "radio"){
                        $(".jmrqflInput:radio").next().hide();
                    }

                    var flg = $(this).is(":checked");
                    if(flg){
                        var sx = $(this).data("sx");
                        if(!StringUtils.isEmpty(sx)){
                            $(this).data("sx", "");
                            $(fwbLi).find(".fwbsx").show();
                            $(fwbLi).find("input:radio[value='" + sx + "']").prop("checked", true);
                        }else{
                            $(fwbLi).find(".fwbsx").toggle();
                            $(fwbLi).find(".fwbsx :radio").removeAttr("checked");
                        }
                    }else{
                        $(fwbLi).find(".fwbsx").hide();
                    }
                });
            })
            td3.append(div);
            tr.append(td1,td2,td3);
            $("#tbody").append(tr);
        }

		/*禁选服务包设置----指定已经签订的服务包*/
        //不支持批量
        $.QuickRemote.AjaxJson(contextRoot + "jtqy/getYqdFwbid", function(result){
            if(result.state){
                //服务包类型_已签订
                var FWBLXS_YQD = result.info.FWBLXS_YQD;
                if(FWBLXS_YQD.indexOf("01")!=-1 || FWBLXS_YQD.indexOf("02")!=-1){ //免费 || 基础
                    $(".dxFwb").removeAttr("checked").prop("disabled","disabled").css("cursor","not-allowed")
                        .parent().css({"color":"#ccc"})
                        .find(".fwbsx").hide();
                }
            }else{
                $.QuickAlert.alertFail({content: result.info.faultInfo});
            }

        }, {SXSJ:bgfwbInfo[0].SXSJ, JSSJ:bgfwbInfo[0].JSSJ, ZJHM:bgfwbInfo[0].ZJHM, QYXXID:bgfwbInfo[0].QYXXID});
	}

	function initView(){
		/* 提交 */
        $("#btnTj").click(function(){
            newFwbid = [],newFwbsx = []; //清空
            var newFwbArr = [],isSFBHYBFWX = false,YBJJZF = 0;
            if($(".fwbUl").QuickValidate()){
                //遍历每个tr，一个tr就是一个患者
                $("#tbody .item").each(function(i){ //tr
                    var fwbidArr = [],fwbsxArr = [];
                    $(".fwbUl").eq(i).find(".jmrqflInput").each(function(){
                        var checked = $(this).is(":checked");
                        if(checked){
                            var value = $(this).val();
                            var mc = $(this).prev().html();
                            fwbidArr.push(value);

                            var fwbsx = $(this).siblings(".fwbsx");
                            var sxmc = $(fwbsx).find("input:radio:checked").parent().text();
                            var value_sx = $(fwbsx).find("input:radio:checked").val();
                            fwbsxArr.push(value_sx);
                            newFwbArr.push(mc + "(" + sxmc + ")");

                            var SFBHYBFWX = $(this).data("SFBHYBFWX");
                            if(SFBHYBFWX==1){
                                YBJJZF += parseFloat($(this).data("YBJJZF"));
                                isSFBHYBFWX = true;
                            }
                        }
                    });
                    newFwbid.push(fwbidArr.join(","));
                    newFwbsx.push(fwbsxArr.join(","));
                })


                //协议确定
//                xyConfirm(newFwbArr);
				//添加医保服务
                addYbfw(isSFBHYBFWX,YBJJZF,newFwbArr);
            }
        })

	    /*返回*/
	    $("#btnFh").click(function(){
            parent.layer.closeAll();
		})
	}

	/* 添加医保服务 */
	function addYbfw(isSFBHYBFWX,YBJJZF,newFwbArr){
        if(isSFBHYBFWX){
            StoreCache.setCache("managerData",{YBJJZF_MAX: YBJJZF,YBFWX_ARR:$("body").data("YBFWX_ARR")});
            parent.layer.open({
                type: 2,
                area: ["60%", "500px"],
                content: contextRoot + 'jtqy/selectYbfwx.html',
                scrollbar: false,
                resize: false,
                title:$($("#selectYbfwxTitle").html()).find("#total").text(YBJJZF).end().prop("outerHTML"),
                end: function(){
                    var YBFWX_ARR = StoreCache.removeCache("YBFWX_ARR");
                    if(!StringUtils.isEmpty(YBFWX_ARR)){
                        $("body").data("YBFWX_ARR", YBFWX_ARR);
                        xyConfirm(newFwbArr);
                    }
                }
            });
        }else{
            $("body").removeData("YBFWX_ARR");
            xyConfirm(newFwbArr);
        }
	}

	/** 新协议确定 */
	function xyConfirm(newFwbArr){
	    var jfMsg = $.extend({},bgfwbInfo[0]);
	    jfMsg.YQYFWB = jfMsg.FWBMC;
        jfMsg.FWBMC = newFwbArr.join(";")
		jfMsg.FWBIDS = newFwbid[0].split(",").join("|");

        StoreCache.setCache("YBFWX_ARR",$("body").data("YBFWX_ARR"));//医保服务项
        StoreCache.setCache("xy_jfMsg",jfMsg);//协议_甲方信息
        StoreCache.setCache("czxxDivId","bgxxDiv");//czxxDivId(注：操作信息展示的Div的Id) 为： bgxxDiv
        parent.layer.open({
            type: 2,
            area: ["800px", "90%"],
            content: contextRoot + 'jtqy/changeXy.html',
            scrollbar: false,
            resize: false,
            title:'新协议确定',
            end: function(){
                var xyContent = StoreCache.getCache("xyContent");
                if(!StringUtils.isEmpty(xyContent)){
                    saveBgfwbData();
				}
            }
        });
	}

    /** 确认保存 */
    function saveBgfwbData(){
        var param = {
            QYXXIDS: JSON.stringify(QYXXIDS),
            NEWFWBID: JSON.stringify(newFwbid),
            NEWFWBSX: JSON.stringify(newFwbsx),
            OLDFWBID: JSON.stringify(FWBIDS),
            OLDFWBSX: JSON.stringify(FWBSX),
            CONTENT: StoreCache.removeCache("xyContent"),
            JMXXIDS: JSON.stringify(JMXXIDS),
            TDIDS: JSON.stringify(TDIDS),
            TDMCS: JSON.stringify(TDMCS),
            XYH: StoreCache.removeCache("XYH"),
            YBFWX_ARR: $("body").data("YBFWX_ARR") || ""
        }
        var bgfwburl = $.QuickUrlUtils.getProjectName()+"/jtqy/bgfwb";
        $.QuickRemote.AjaxJson(bgfwburl,function(result){
            if (result["state"]) {
                $.QuickAlert.alertNormal("变更服务成功！",{callback: function(){
                    StoreCache.setCache("JTQY_OPT_SUCCESS", "true");
                    parent.layer.closeAll();
                }});
            } else {
                $.QuickAlert.alertFail({content : info["faultInfo"]});
            }
        },param);
    }

    /** 校验服务包 */
    function validateFwb(){
        var valid = "";
        var hasChecked = false;
        $(this).find(".jmrqflInput").each(function(){
            var checked = $(this).is(":checked");
            if(checked){
                hasChecked = true;
                var fwbsx = $(this).siblings(".fwbsx");
                var value = $(fwbsx).find("input:radio:checked").val();
                if(StringUtils.isEmpty(value)){
                    valid = "请选择服务包适用于个人或家庭";
                }
            }
        });
        if(!hasChecked){
            valid = "请选择服务包";
        }
        return valid;
    }
</script>

<body class="easyui-layout" fit="true" border="false">
<div region="center" style="height:150px;">
		<div style="font-size:16px;padding-left:2.5%">请选择新服务包</div>
		<!-- <div class="layui-form-item" style="text-align: center;">
			<h1 id="JLS"></h1>
		</div> -->
		 <table class="layui-table" style="width:95%;margin:0 auto">
		  <colgroup>
		    <col width="100">
		    <col width="150">
		    <col>
		  </colgroup>
		  <thead>
		    <tr>
		      <th style="text-align:center">姓名</th>
		      <th>原服务包</th>
		      <th>变更服务包</th>
		    </tr> 
		  </thead>
		  <tbody id="tbody"></tbody>
		</table>
	</div>
	<div region="south" style="height:40px;line-height:40px">
		<div class="layui-form-item" style="position: absolute;width: 100%;margin-bottom: 0px;bottom: 0px;text-align: center;">
			<div class="layui-btn" lay-submit lay-filter="formDemo" style="margin-left: 0px;" id="btnTj">提交</div>
			<button type="reset" class="layui-btn layui-btn-primary" id="btnFh">返回</button>
		</div>
	</div>
</body>
<script type="text/html" id="fwbLi">
	<li class="fwbLi">
		<label></label>
		<input class="jmrqflInput" type="checkbox" name="InputFwb">
		<span class="fwbsx" style="display: inline-block;height: 100%;padding-left: 2px;border-left: 1px dashed #ccc;">
			<span><input type="radio" value="1">个人</span>
			<span><input type="radio" value="2">家庭</span>
		</span>
	</li>
</script>
<!--医保服务项——弹窗的title -->
<script type="text/html" id="selectYbfwxTitle">
	<div>
		<span>您选择了包含医保服务项的服务包，请自主选择医保服务项</span>
		<span style="margin-left:20px;">总价在</span>
		<span style="font-size:16px;color:blue" id="total"></span>
		<span>元以内</span>
	</div>
</script>
</html>
